package com.alex;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 配置单个数据源
 * 1. 使用actuator时, 会在console中打印出endpoint:  Exposing 15 endpoint(s) beneath base path '/actuator';
 *    默认只会暴露两个endpoint; 需要添加配置中的management.endpoints.web.exposure.include: "*"; 才会暴露出所有的actuator的
 *    endpoint.
 */
@Slf4j
@SpringBootApplication
public class SpringBucksApplication implements CommandLineRunner {

    @Autowired
    private DataSource dataSource;

    public static void main(String[] args) {
        SpringApplication.run(SpringBucksApplication.class, args);
    }

    public void run(String... args) throws Exception {
        showConnection();
    }

    private void showConnection() throws SQLException {
        // HikariDataSource 因为使用的是h2 DB
        log.info(dataSource.toString());
        Connection connection = dataSource.getConnection();

        // HikariProxyConnection@2110785285 wrapping conn0: url=jdbc:h2:mem:testdb user=SA
        // spring boot会自动根据引入的h2数据库, 自动配置相关的配置.
        log.info(connection.toString());
        connection.close();
    }
}
